<template>
  <div class="com-container">
    <div class="com-chart" ref="age_ref"></div>
  </div>
</template>

<script>
import chalk from "../../../public/static/theme/chalk"
export default {
  props: ['admissionyear','academy','status'],
  watch: {
    admissionyear: {
      handler(newValue,oldValue) {
        this.getData()
      }
    },
    academy: {
      handler(newValue,oldValue) {
        this.getData()
      }
    },
    status: {
      handler(newValue,oldValue) {
        this.getData()
      }
    }
  },
  data() {
    return {
      chartInstance: null,
      allData: []
    };
  },
  mounted() {
    this.initChart();
    this.getData();
    // 屏幕大小改变的监听方法，当屏幕大小发生改变时，将会调用screenAdapter()方法
    window.addEventListener("resize", this.screenAdapter());
    // 首次加载时调用屏幕适应方法
    this.screenAdapter();
  },
  //在组件销毁时，进行监听事件的取消
  destroyed() {
    window.removeEventListener("resize", this.screenAdapter());
  },
  methods: {
    // 初始化echartInstance对象
    initChart() {
      this.chartInstance = this.$echarts.init(this.$refs.age_ref,'chalk');
      const initOption = {
        title: {
          text:'年龄分布',
        },

        series: [
          {
            name: 'Nightingale Chart',
            type: 'pie',
            radius: [50, 250],
            center: ['50%', '50%'],
            roseType: 'area',
            label: {
              show: true,
              position:'outside',
              formatter:'{b} \n {c}人 ({d}%)'
            },
            itemStyle: {
              borderRadius: 8
            },
          }
        ]
      };
      this.chartInstance.setOption(initOption);
    },
    // 数据
    getData() {
      this.request.get("/stuScreen/age",{
        params: {
          admissionyear: this.admissionyear,
          academy: this.academy
        }
      }).then(res=>{
        if(res.code==20041) {
          this.allData=res.data
        }
      }).finally(()=>{
        this.updateChart()
      });
    },
    // 更新图表
    updateChart() {
      const dataOption = {
        series: [
          {
            data:this.allData
          }
        ]
      };
      this.chartInstance.setOption(dataOption);
    },
    screenAdapter() {
      const sizeModel = (this.$refs.age_ref.offsetWidth / 100) * 7.2;

      const adapterOption = {

        title: {
          textStyle: {
            fontSize: sizeModel/2
          }
        },
        series: [
          {
            radius: [sizeModel/2, sizeModel*2],
            itemStyle: {
              borderRadius: sizeModel*0.15
            },
          }
        ]
      };
      this.chartInstance.setOption(adapterOption);
      this.chartInstance.resize();
    },
  },
};
</script>
// 设置为scoped，仅在本页面生效
<style lang="less" scoped>

</style>
